﻿#region namespaces

using System;

using SLIITPro.Data.Extension;
using SLIITPro.Data.Entity;
using SLIITPro.Core;

#endregion

namespace SLIITPro.PasswordRecover
{
    public partial class Requestpw : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private UserExtension _userExtension;
        private Users _users;
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            _userExtension = new UserExtension();
            if (!_userExtension.IsEmailExist(txtEmail.Text.Trim()))
            {
                lblMessage.Text = "You have Entered Invalide Email or our system does not exsist you login informaion";
                return;
            }
            var passCode = Utility.GenerateId();
            var resetUrl = string.Format("{0}PasswordRecover/reseturl.aspx?p={1}", CoreSettings.AbsoluteUrl, passCode);
            var userdata = _userExtension.SelectUser("Email", txtEmail.Text);
            _users = new Users(userdata.Rows[0]);
            using (var template = LoadControl(CoreSettings.TemplateRoot + "PRequestMail.ascx"))
            {
                ((IPassTemplate)template).UserName = _users.FirstName + " " + _users.LastName;
                ((IPassTemplate)template).NevigateUrl = resetUrl;
                ((IPassTemplate)template).UrlText = resetUrl;
                ((IPassTemplate)template).PassCode = passCode;
                ((IPassTemplate)template).RequesteDate = string.Format("{0} : ({1})",
                                                                        DateTime.Now.ToLongDateString(),
                                                                        DateTime.Now.ToShortTimeString());
                _userExtension.SetUserPasswordRequest(_users.UserId, passCode, DateTime.Now);
                var body = Utility.BuildTemplate(template);
                Utility.SendEmail(_users.Email, "LBIS", string.Format("Passowrd Reset Confirmation (Code:{0})", passCode), body);
                txtEmail.Text = string.Empty;
                lblMessage.Text = "Password reset confirmation sent succeeded";
            }
        }
    }
}